#include <misaka.h>

/*payload format
|----------------------------------
|magic|type|payloadLen|payloadData|
|----------------------------------
*  32 + 16 + 16 = 64   
*/

#define MAGIC_STR "01234567890123456789012345678912"
#define MAGIC_STR_SIZE  (32)
#define MAGIC_HEADER_SIZE (64) 

//init parser in peer
int normal_parser_init(struct peer *peer){
    peer->header_size = MAGIC_HEADER_SIZE;
    peer->body_size = 0;
    peer->packet_size = MAGIC_HEADER_SIZE;
    return 0;
}

//just pop out this packet
int normal_parser_unpack(struct stream *s, struct peer *peer){
    int length = 0;

    if(peer->body_size == 0){

        length = stream_get_endp(s) - stream_get_getp(s);
        if(length < MAGIC_HEADER_SIZE){
            return IO_PARTIAL; 
        }else{
            if(0 != memcmp(STREAM_PNT(s), MAGIC_STR, MAGIC_STR_SIZE)){
                return IO_CLOSE;
            }
        }

        s->getp += MAGIC_STR_SIZE;
        s->type = stream_getl(s);
        length = stream_getl(s);

        peer->body_size = length;
        peer->packet_size = length;
        return IO_PARTIAL;
    }
    return IO_PACKET;
}

//reverse header for pack
int normal_parser_reverse(struct stream *s){
    s->getp +=  MAGIC_HEADER_SIZE;
    return 0;
}

//init message header for the packet wait to send
int normal_parser_pack(struct stream *s, struct peer *peer){
    int body_size = stream_get_endp(s) - stream_get_getp(s);
    s->getp = 0;
    stream_put (s, MAGIC_STR, MAGIC_STR_SIZE);
    stream_putl(s, s->type);
    stream_putl(s, body_size);
    s->getp = 0;
    return IO_PACKET;
}
